package com.talent.jiwen.my;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.talent.jiwen.R;
import com.talent.jiwen.base.BaseActivity;
import com.talent.jiwen.http.common.ActivityLifeCycleEvent;
import com.talent.jiwen.http.common.ApiFactory;
import com.talent.jiwen.http.common.HttpUtil;
import com.talent.jiwen.http.common.ProgressSubscriber;
import com.talent.jiwen.util.ImageUtil;
import com.talent.jiwen.util.MyToast;
import com.talent.jiwen.util.QRCodeUtil;
import com.talent.jiwen.util.SPConstant;
import com.talent.jiwen.util.SPUtil;
import com.talent.jiwen.util.Validators;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Observable;

/**
 * Created by qyqx on 2018/8/17.
 * 个人信息
 */

public class PersonInfoActivity extends BaseActivity {

    @BindView(R.id.rl_up_head)
    RelativeLayout rl_up_head;
    @BindView(R.id.img_head)
    ImageView img_head;
    @BindView(R.id.nameLayout)
    RelativeLayout nameLayout;
    @BindView(R.id.txt_name)
    TextView txt_name;
//    @BindView(R.id.qrCodeLayout)
//    RelativeLayout qrCodeLayout;

    public static final String REFRESH_INFO = "refresh.info";

    @Override
    protected int provideContentViewId() {
        return R.layout.activiy_person_info;
    }

    @Override
    public String titleName() {
        return "个人信息";
    }

    @Override
    protected void initData() {
        registEventBus();

        ImageUtil.loadCircleImage(PersonInfoActivity.this, SPUtil.getUserHeadImg(),img_head);
        txt_name.setText(SPUtil.getUserName());
    }

    @Subscribe
    public void onEvent(String flag) {
        if (flag.equals(REFRESH_INFO)) {
            ImageUtil.loadCircleImage(PersonInfoActivity.this, SPUtil.getUserHeadImg(),img_head);
            txt_name.setText(SPUtil.getUserName());
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unRegistEventBus();
    }

    @OnClick({R.id.rl_up_head , R.id.nameLayout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_up_head:
                ImageUtil.pickPhoto(PersonInfoActivity.this , 1 , null , true);
                break;
            case R.id.nameLayout:
                ChangeInfoActivity.startChangeInfoActivity(PersonInfoActivity.this , txt_name.getText().toString());
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    if (!Validators.isEmpty(PictureSelector.obtainMultipleResult(data))){
                        String path = PictureSelector.obtainMultipleResult(data).get(0).getCompressPath();
                        ImageUtil.upLoadSingleImg(path, new ImageUtil.ImgUpLoadListener() {
                            @Override
                            public void OnSuccess(String successUrl) {
                                updateStudentInfo(successUrl , "");
                            }

                            @Override
                            public void OnError(String message) {

                            }

                            @Override
                            public void OnListUpSuccess(List<String> successFileList) {

                            }
                        });
                    }
                    break;
            }
        }
    }


    /**更新学生个人信息
     *
     * @param userImg
     * @param name
     */
    private void updateStudentInfo(final String userImg , final String name){
        Map<String, String> map = new HashMap<>();
        map.put("studentId" , SPUtil.getUserId());
        if (!Validators.isEmpty(userImg)){
            map.put("studentHeadImage" , userImg);
        }

        if (!Validators.isEmpty(name)){
            map.put("studentName" , name);
        }

        Observable observable = ApiFactory.getEducationApiSingleton().updateStudentInfo(SPUtil.stringOut(SPConstant.TOKEN) , map);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscriber<Void>(PersonInfoActivity.this) {
            @Override
            protected void _onNext(Void v) {
                if (!Validators.isEmpty(userImg)){
                    SPUtil.stringIn(SPConstant.USER_HEAD_URL , userImg);
                }

                if (!Validators.isEmpty(name)){
                    SPUtil.stringIn(SPConstant.USER_NAME , name);
                }

                ImageUtil.loadCircleImage(PersonInfoActivity.this, SPUtil.getUserHeadImg(),img_head);
                txt_name.setText(SPUtil.getUserName());

                EventBus.getDefault().post(REFRESH_INFO);
            }

            @Override
            protected void _onError(String message) {
                showToast(message);
            }
        }, ActivityLifeCycleEvent.DESTROY, lifecycleSubject,true);
    }

}
